$OpenBSD: patch-buffer_c,v 1.3 2017/04/13 21:21:43 naddy Exp $
--- buffer.c.orig	Sat Oct 30 18:54:47 1999
+++ buffer.c	Thu Apr 13 23:15:40 2017
@@ -117,7 +117,8 @@
 static char *rcsid = "$Header: /home/cvs/ports/misc/buffer/patches/patch-buffer_c,v 1.3 2017/04/13 21:21:43 naddy Exp $";
 #endif
 
-extern char *shmat();
+#include <string.h>
+#include <stdlib.h>
 
 /* General macros */
 #define TRUE 1
@@ -168,7 +169,7 @@ int max_shmem = DEF_SHMEM;
 /* the shared memory id of the buffer */
 int buffer_id = NONE;
 struct block {
-	int bytes;
+	int *bytes;
 	char *data;
 } *curr_block;
 
@@ -185,12 +186,13 @@ struct buffer {
 	int next_block_in;
 	int next_block_out;
 
-	struct block block[ MAX_BLOCKS ];
-
 	/* These actual space for the blocks is here - the array extends
 	 * pass 1 */
 	char data_space[ 1 ];
 } *pbuffer = NO_BUFFER;
+
+struct block block[ MAX_BLOCKS ];
+
 int buffer_size;
 
 int fdin	= 0;
@@ -447,7 +449,7 @@ buffer_allocate()
 
 	/* Allow for the data space */
 	buffer_size = sizeof( struct buffer ) +
-		((blocks * blocksize) - sizeof( char ));
+		(blocks * (sizeof(int) + blocksize) - sizeof( char ));
 
 	/* Create the space for the buffer */
 	buffer_id = shmget( IPC_PRIVATE,
@@ -490,6 +492,7 @@ buffer_allocate()
 	pbuffer = NO_BUFFER;
 }
 
+void
 buffer_remove()
 {
 	static char removing = FALSE;
@@ -533,8 +536,13 @@ get_buffer()
 
 	/* Setup the data space pointers */
 	for( b = 0; b < blocks; b++ )
-		pbuffer->block[ b ].data =
-			&pbuffer->data_space[ b * blocksize ];
+	{
+		block[ b ].bytes =
+			(int *)&pbuffer->data_space[ b * blocksize + b * sizeof(int)];
+		block[ b ].data =
+			&pbuffer->data_space[ b * blocksize + 
+				( b + 1 ) * sizeof(int)];
+	}
 
 }
 
@@ -592,7 +600,7 @@ get_next_free_block()
 	/* Maybe wait till there is room in the buffer */
 	lock( pbuffer->semid, pbuffer->blocks_free_lock );
 
-	curr_block = &pbuffer->block[ pbuffer->next_block_in ];
+	curr_block = &block[ pbuffer->next_block_in ];
 
 	pbuffer->next_block_in = INC( pbuffer->next_block_in );
 }
@@ -605,7 +613,7 @@ fill_block()
 	static char eof_reached = 0;
 	
 	if( eof_reached ){
-		curr_block->bytes = 0;
+		*curr_block->bytes = 0;
 		unlock( pbuffer->semid, pbuffer->blocks_used_lock );
 		return 0;
 	}
@@ -631,10 +639,10 @@ fill_block()
 
 	/* number of bytes available. Zero will be taken as eof */
 	if( !padblock || toread == blocksize )
-		curr_block->bytes = blocksize - toread;
+		*curr_block->bytes = blocksize - toread;
 	else {
 		if( toread ) bzero( start, toread );
-		curr_block->bytes = blocksize;
+		*curr_block->bytes = blocksize;
 	}
 
 	if( debug > 1 )
@@ -642,7 +650,7 @@ fill_block()
 
 	unlock( pbuffer->semid, pbuffer->blocks_used_lock );
 
-	return curr_block->bytes;
+	return *curr_block->bytes;
 }
 
 /* Write the buffer to stdout */
@@ -697,14 +705,14 @@ get_next_filled_block()
 	/* Hang till some data is available */
 	lock( pbuffer->semid, pbuffer->blocks_used_lock );
 
-	curr_block = &pbuffer->block[ pbuffer->next_block_out ];
+	curr_block = &block[ pbuffer->next_block_out ];
 
 	pbuffer->next_block_out = INC( pbuffer->next_block_out );
 }
 
 data_to_write()
 {
-	return curr_block->bytes;
+	return *curr_block->bytes;
 }
 
 write_blocks_to_stdout( filled, first_block )
@@ -714,7 +722,7 @@ write_blocks_to_stdout( filled, first_block )
 	pbuffer->next_block_out = first_block;
 
 	while( filled-- ){
-		curr_block = &pbuffer->block[ pbuffer->next_block_out ];
+		curr_block = &block[ pbuffer->next_block_out ];
 		pbuffer->next_block_out = INC( pbuffer->next_block_out );
 		write_block_to_stdout();
 	}
@@ -734,7 +742,7 @@ write_block_to_stdout()
 		next_k = showevery;
 	}
 
-	if( (written = write( fdout, curr_block->data, curr_block->bytes )) != curr_block->bytes ){
+	if( (written = write( fdout, curr_block->data, *curr_block->bytes )) != *curr_block->bytes ){
 		report_proc();
 		perror( "write of data failed" );
 		fprintf( stderr, "bytes to write=%d, bytes written=%d, total written %10luK\n", curr_block->bytes, written, outk );
@@ -745,7 +753,7 @@ write_block_to_stdout()
 		usleep( write_pause );
 	}
 
-	out = curr_block->bytes / 1024;
+	out = *curr_block->bytes / 1024;
 	outk += out;
 	last_gb += out;
 
